package com.prs.crm.action.crm;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.crm.CodeNumber;
import com.prs.crm.domain.crm.Configure;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerContract;
import com.prs.crm.domain.crm.CustomerProject;
import com.prs.crm.domain.crm.DocumenTary;
import com.prs.crm.domain.crm.DocumenTaryStyle;
import com.prs.crm.domain.crm.Fault;
import com.prs.crm.domain.crm.ProjectContract;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.crm.CodeNumberService;
import com.prs.crm.service.crm.ConfigureService;
import com.prs.crm.service.crm.CustomerProjectService;
import com.prs.crm.service.crm.CustomerService;
import com.prs.crm.service.crm.DocumenTaryService;
import com.prs.crm.service.crm.DocumenTaryStyleService;
import com.prs.crm.service.crm.FaultService;
import com.prs.crm.service.crm.ProjectContractService;

public class CustomerProjectAction  extends BaseAction{
private static final long serialVersionUID = 1L;
	
	private Customer customer;
	private CustomerService customerService;
	private CustomerProject customerProject;
	private ProjectContract projectContract;
	private DocumenTary documenTary;
	private PaginationSupport pagination;
	private Integer[] customerIds;
	private Integer[] projectIds;
	private Integer[] contractIds;
	private Integer[] proconfigIds;
	private CodeNumber codeNumber;
	private CodeNumberService codeNumberService;
	private File upload;
	private String uplodS;
	private String uploadFileName;
	
	private ProjectContractService projectContractService;
	private DocumenTaryService documenTaryService;
	private CustomerProjectService customerProjectService;
	private List<Configure> configures;
	private List<DocumenTary> tarys;
	private List<ProjectContract> proes;
	private List<Fault> faults;
	private List<DocumenTaryStyle> styles;
	private DocumenTaryStyleService documenTaryStyleService;
	
	private ConfigureService configureService;
	private FaultService faultService;
	//配置单信息
	private String[] shebeiName; 
	private String[] modelstyle;
	private String[] nums;
	private String[] remark;
	
	@Action(value = "saveProject3", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerAndProject"),
			@Result(name = "error", type = "chain", location = "editProject") })
	public String saveProject3(){
		if(upload!=null){
			customerProject.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		customerProject.setCustomer(customer);
		customerProject.setDay(new Date());
		this.customerProjectService.save(customerProject);
		//编号
		String edit=this.codeNumber.getEdit();
		String sample=this.codeNumber.getSample();
		String[] ee=edit.split(",");
		String[] ss=sample.split(",");
		List<String> list=new ArrayList<String>();
		for(int i=0;i<ee.length;i++){
			String samp="";
			if(ee[i].equals("##")){
				int bb=Integer.parseInt(ss[i]);
				bb=bb+1;
				String b="";
				for(int a=0;a<ss[i].length()-String.valueOf(bb).length();a++){
					b=b+"0";
				}
				samp=b+bb;
				ss[i]=samp;
			}
			if(i==ee.length-1){
				list.add(ss[i]);
			}else{
				list.add(ss[i]+",");
			}
		}
		String d="";
		String q="";
		for(int i=0;i<list.size();i++){
			d=d+list.get(i);
		}
		String[] dd=d.split(",");
		for(int i=0;i<dd.length;i++){
			q=q+dd[i];
		}
		this.codeNumber.setSample(d);
		this.codeNumber.setCodeSample(q);
		int now=Integer.parseInt(codeNumber.getNowCode())+1;
		this.codeNumber.setNowCode(String.valueOf(now));
		this.codeNumberService.submit(codeNumber);
		this.setPrompt("录入项目成功");
		return SUCCESS;
	}
	
	@Action(value = "saveProject2", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerProject"),
			@Result(name = "error", type = "chain", location = "editProject") })
	public String saveProject2(){
		if(upload!=null){
			customerProject.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		customerProject.setCustomer(customer);
		customerProject.setDay(new Date());
		this.customerProjectService.save(customerProject);
		//编号
		String edit=this.codeNumber.getEdit();
		String sample=this.codeNumber.getSample();
		String[] ee=edit.split(",");
		String[] ss=sample.split(",");
		List<String> list=new ArrayList<String>();
		for(int i=0;i<ee.length;i++){
			String samp="";
			if(ee[i].equals("##")){
				int bb=Integer.parseInt(ss[i]);
				bb=bb+1;
				String b="";
				for(int a=0;a<ss[i].length()-String.valueOf(bb).length();a++){
					b=b+"0";
				}
				samp=b+bb;
				ss[i]=samp;
			}
			if(i==ee.length-1){
				list.add(ss[i]);
			}else{
				list.add(ss[i]+",");
			}
		}
		String d="";
		String q="";
		for(int i=0;i<list.size();i++){
			d=d+list.get(i);
		}
		String[] dd=d.split(",");
		for(int i=0;i<dd.length;i++){
			q=q+dd[i];
		}
		this.codeNumber.setSample(d);
		this.codeNumber.setCodeSample(q);
		int now=Integer.parseInt(codeNumber.getNowCode())+1;
		this.codeNumber.setNowCode(String.valueOf(now));
		this.codeNumberService.submit(codeNumber);
		this.setPrompt("录入项目成功");
		return SUCCESS;
	}
	@Action(value = "saveProject", results = {
			@Result(name = "success", type = "redirect", location = "viewCustomer?customer=${customer.getId()}"),
			@Result(name = "error", type = "chain", location = "editProject") })
	public String save(){
		if(customerProject.getId()==null){
			customerProject.setCustomer(customer);
			customerProject.setDay(new Date());
			this.customerProjectService.save(customerProject);
		}else{
			customerProject.setCustomer(customer);
			customerProject.setDay(new Date());
			this.customerProjectService.submit(customerProject);
		}
		//编号
		String edit=this.codeNumber.getEdit();
		String sample=this.codeNumber.getSample();
		String[] ee=edit.split(",");
		String[] ss=sample.split(",");
		List<String> list=new ArrayList<String>();
		for(int i=0;i<ee.length;i++){
			String samp="";
			if(ee[i].equals("##")){
				int bb=Integer.parseInt(ss[i]);
				bb=bb+1;
				String b="";
				for(int a=0;a<ss[i].length()-String.valueOf(bb).length();a++){
					b=b+"0";
				}
				samp=b+bb;
				ss[i]=samp;
			}
			if(i==ee.length-1){
				list.add(ss[i]);
			}else{
				list.add(ss[i]+",");
			}
		}
		String d="";
		String q="";
		for(int i=0;i<list.size();i++){
			d=d+list.get(i);
		}
		String[] dd=d.split(",");
		for(int i=0;i<dd.length;i++){
			q=q+dd[i];
		}
		this.codeNumber.setSample(d);
		this.codeNumber.setCodeSample(q);
		int now=Integer.parseInt(codeNumber.getNowCode())+1;
		this.codeNumber.setNowCode(String.valueOf(now));
		this.codeNumberService.submit(codeNumber);
		this.setPrompt("录入项目成功");
		return SUCCESS;
	}
	@Action(value = "saveProjectContract2", results = {
			@Result(name = "success", type = "redirect", location = "viewCustomerProject?customerProject=${customerProject.getId()}"),
			@Result(name = "error", type = "chain", location = "editProjectContract") })
	public String saveProjectContract2(){
		//配置单
		//删除配置单
		if(this.proconfigIds!=null){
			this.configureService.batchRemove(proconfigIds);
		}
		if(projectContract.getId()==null){
			customerProject.setRePerson(projectContract.getReperson());
			this.getCustomerProjectService().submit(customerProject);
			this.projectContract.setCustomerProject(customerProject);
			this.projectContractService.save(projectContract);
		}else{
			customerProject.setRePerson(projectContract.getReperson());
			this.getCustomerProjectService().submit(customerProject);
			this.projectContract.setCustomerProject(customerProject);
			this.projectContractService.submit(projectContract);
		}
		//添加配置单
		if(this.shebeiName!=null){
			Configure conf=null;
			for(int i=0;i<shebeiName.length;i++){
				if(shebeiName[i]==null || shebeiName[i].equals("")){
					this.addActionError("请将配置单填写完");
					return ERROR;
				}
				conf=new Configure();
				conf.setDeciveName(shebeiName[i]);
				conf.setStyle(modelstyle[i]);
				conf.setNums(nums[i]);
				conf.setRemark(remark[i]);
				conf.setCustomerProject(customerProject);
				conf.setProjectContract(projectContract);
				this.configureService.save(conf);
			}
		}
		//编号
		String edit=this.codeNumber.getEdit();
		String sample=this.codeNumber.getSample();
		String[] ee=edit.split(",");
		String[] ss=sample.split(",");
		List<String> list=new ArrayList<String>();
		for(int i=0;i<ee.length;i++){
			String samp="";
			if(ee[i].equals("##")){
				int bb=Integer.parseInt(ss[i]);
				bb=bb+1;
				String b="";
				for(int a=0;a<ss[i].length()-String.valueOf(bb).length();a++){
					b=b+"0";
				}
				samp=b+bb;
				ss[i]=samp;
			}
			if(i==ee.length-1){
				list.add(ss[i]);
			}else{
				list.add(ss[i]+",");
			}
		}
		String d="";
		String q="";
		for(int i=0;i<list.size();i++){
			d=d+list.get(i);
		}
		String[] dd=d.split(",");
		for(int i=0;i<dd.length;i++){
			q=q+dd[i];
		}
		this.codeNumber.setSample(d);
		this.codeNumber.setCodeSample(q);
		int now=Integer.parseInt(codeNumber.getNowCode())+1;
		this.codeNumber.setNowCode(String.valueOf(now));
		this.codeNumberService.submit(codeNumber);
		this.setPrompt("录入合同成功");
		return SUCCESS;
	}
	@Action(value = "saveProjectContract", results = {
			@Result(name = "success", type = "redirect", location = "listProjectContract"),
			@Result(name = "error", type = "chain", location = "editProjectContract") })
	public String saveProjectContract(){
		if(upload!=null){
			customerProject.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		//配置单
		//删除配置单
		if(this.proconfigIds!=null){
			this.configureService.batchRemove(proconfigIds);
		}
		if(projectContract.getId()==null){
			customerProject.setRePerson(projectContract.getReperson());
			this.getCustomerProjectService().submit(customerProject);
			this.projectContract.setCustomerProject(customerProject);
			this.projectContractService.save(projectContract);
			//添加配置单
			if(this.shebeiName!=null){
				Configure conf=null;
				for(int i=0;i<shebeiName.length;i++){
					if(shebeiName[i]==null || shebeiName[i].equals("")){
						this.addActionError("请将配置单填写完");
						return ERROR;
					}
					conf=new Configure();
					conf.setDeciveName(shebeiName[i]);
					conf.setStyle(modelstyle[i]);
					conf.setNums(nums[i]);
					conf.setRemark(remark[i]);
					conf.setCustomerProject(customerProject);
					conf.setProjectContract(projectContract);
					this.configureService.save(conf);
				}
			}
			//编号
			String edit=this.codeNumber.getEdit();
			String sample=this.codeNumber.getSample();
			String[] ee=edit.split(",");
			String[] ss=sample.split(",");
			List<String> list=new ArrayList<String>();
			for(int i=0;i<ee.length;i++){
				String samp="";
				if(ee[i].equals("##")){
					int bb=Integer.parseInt(ss[i]);
					bb=bb+1;
					String b="";
					for(int a=0;a<ss[i].length()-String.valueOf(bb).length();a++){
						b=b+"0";
					}
					samp=b+bb;
					ss[i]=samp;
				}
				if(i==ee.length-1){
					list.add(ss[i]);
				}else{
					list.add(ss[i]+",");
				}
			}
			String d="";
			String q="";
			for(int i=0;i<list.size();i++){
				d=d+list.get(i);
			}
			String[] dd=d.split(",");
			for(int i=0;i<dd.length;i++){
				q=q+dd[i];
			}
			this.codeNumber.setSample(d);
			this.codeNumber.setCodeSample(q);
			int now=Integer.parseInt(codeNumber.getNowCode())+1;
			this.codeNumber.setNowCode(String.valueOf(now));
			this.codeNumberService.submit(codeNumber);
		}else{
			projectContract.getCustomerProject().setRePerson(projectContract.getReperson());
			this.getCustomerProjectService().submit(projectContract.getCustomerProject());
			this.projectContract.setCustomerProject(projectContract.getCustomerProject());
			this.projectContractService.submit(projectContract);
		}
		
		this.setPrompt("录入合同成功");
		return SUCCESS;
	}
	@Action("listCustomerProject")
	public String listCustomerContract(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerProject.class);
		if(customerIds!=null){
			customer=this.customerService.get(customerIds[0]);
			criteria.add(Restrictions.eq("customer", customer));
		}
		if(customer!=null){
			criteria.add(Restrictions.eq("customer", customer));
		}
		Person per = AppContext.getAccountContext().currentUser();
		criteria.createAlias("recorder", "rd");
		criteria.add(Restrictions.or(Restrictions.like("person","%"+per.getResume().getName()+"%"), Restrictions.or(Restrictions.like("shareperson","%"+per.getResume().getName()+"%"), Restrictions.eq("rd.id", per.getId()))));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.customerProjectService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("selectCustomerProject")
	public String selectCustomerContract(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerProject.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.customerProjectService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listProjectFault")
	public String listProjectFault(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerProject.class);
		criteria.add(Restrictions.like("rePerson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.customerProjectService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("viewCustomerProject")
	public String viewCustomerContract(){
		if(projectIds!=null){
			this.customerProject=this.customerProjectService.get(projectIds[0]);
		}
		this.customer=this.customerProject.getCustomer();
		this.proes=this.projectContractService.getContractByProject(customerProject);
		this.faults=this.faultService.getFaultByProject(customerProject);
		this.tarys=this.documenTaryService.getDocumenTaryByProject(customerProject);
		//查询项目下的所有配置单
		configures=this.configureService.getConfigureByPro(customerProject);
		return SUCCESS;
	}
	@Action(value = "removeCustomerProject", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerProject"),
			@Result(name = "error", type = "chain", location = "listCustomerProject") })
	public String removeCustomerProject(){
		for(int i=0;i<projectIds.length;i++){
			CustomerProject pro=this.customerProjectService.get(projectIds[i]);
			//查询项目的合同
			List<ProjectContract> cons=this.projectContractService.getContractByProject(pro);
			if(cons!=null){
				for(ProjectContract con:cons){
					projectContractService.remove(con);
				}
			}
			//查询项目的跟单
			List<DocumenTary> docs=this.documenTaryService.getDocumenTaryByProject(pro);
			if(docs!=null){
				for(DocumenTary doc:docs){
					this.documenTaryService.remove(doc);
				}
			}
			customerProjectService.remove(pro);
		}
		return SUCCESS;
	}
	@Action("editDocumenTaryXM")
	public String editDocumenTaryXM(){
		if(projectIds!=null){
			this.customerProject=this.customerProjectService.get(projectIds[0]);
		}
		this.styles=this.documenTaryStyleService.getAllStyles();
		return SUCCESS;
	}
	
	@Action(value = "saveDocumenTaryXM", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerProject"),
			@Result(name = "error", type = "chain", location = "editDocumenTaryXM") })
	public String saveDocumenTaryXM(){
		this.documenTary.setCustomerProject(customerProject);
		this.documenTaryService.save(documenTary);
		this.setPrompt("添加跟单成功");
		return SUCCESS;
	}
	@Action(value = "saveDocumenTaryXM2", results = {
			@Result(name = "success", type = "redirect", location = "viewCustomerProject?customerProject=${customerProject.getId()}"),
			@Result(name = "error", type = "chain", location = "editDocumenTaryXM") })
	public String saveDocumenTaryXM2(){
		this.documenTary.setCustomerProject(customerProject);
		this.documenTaryService.save(documenTary);
		this.setPrompt("添加跟单成功");
		return SUCCESS;
	}
	
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public CustomerProject getCustomerProject() {
		return customerProject;
	}
	public void setCustomerProject(CustomerProject customerProject) {
		this.customerProject = customerProject;
	}
	public CustomerProjectService getCustomerProjectService() {
		return customerProjectService;
	}

	public void setCustomerProjectService(
			CustomerProjectService customerProjectService) {
		this.customerProjectService = customerProjectService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getProjectIds() {
		return projectIds;
	}
	public void setProjectIds(Integer[] projectIds) {
		this.projectIds = projectIds;
	}
	public DocumenTary getDocumenTary() {
		return documenTary;
	}
	public void setDocumenTary(DocumenTary documenTary) {
		this.documenTary = documenTary;
	}
	public DocumenTaryService getDocumenTaryService() {
		return documenTaryService;
	}
	public void setDocumenTaryService(DocumenTaryService documenTaryService) {
		this.documenTaryService = documenTaryService;
	}
	public ProjectContract getProjectContract() {
		return projectContract;
	}
	public void setProjectContract(ProjectContract projectContract) {
		this.projectContract = projectContract;
	}
	public ProjectContractService getProjectContractService() {
		return projectContractService;
	}
	public void setProjectContractService(
			ProjectContractService projectContractService) {
		this.projectContractService = projectContractService;
	}
	
	public List<Configure> getConfigures() {
		return configures;
	}
	public void setConfigures(List<Configure> configures) {
		this.configures = configures;
	}
	public String[] getShebeiName() {
		return shebeiName;
	}
	public void setShebeiName(String[] shebeiName) {
		this.shebeiName = shebeiName;
	}
	public String[] getModelstyle() {
		return modelstyle;
	}
	public void setModelstyle(String[] modelstyle) {
		this.modelstyle = modelstyle;
	}
	
	public String[] getNums() {
		return nums;
	}
	public void setNums(String[] nums) {
		this.nums = nums;
	}
	public String[] getRemark() {
		return remark;
	}
	public void setRemark(String[] remark) {
		this.remark = remark;
	}
	public ConfigureService getConfigureService() {
		return configureService;
	}
	public void setConfigureService(ConfigureService configureService) {
		this.configureService = configureService;
	}
	public Integer[] getContractIds() {
		return contractIds;
	}
	public void setContractIds(Integer[] contractIds) {
		this.contractIds = contractIds;
	}
	public Integer[] getCustomerIds() {
		return customerIds;
	}
	public void setCustomerIds(Integer[] customerIds) {
		this.customerIds = customerIds;
	}
	public CustomerService getCustomerService() {
		return customerService;
	}
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	public List<DocumenTary> getTarys() {
		return tarys;
	}
	public void setTarys(List<DocumenTary> tarys) {
		this.tarys = tarys;
	}
	public List<ProjectContract> getProes() {
		return proes;
	}
	public void setProes(List<ProjectContract> proes) {
		this.proes = proes;
	}
	public List<Fault> getFaults() {
		return faults;
	}
	public void setFaults(List<Fault> faults) {
		this.faults = faults;
	}
	public FaultService getFaultService() {
		return faultService;
	}
	public void setFaultService(FaultService faultService) {
		this.faultService = faultService;
	}
	public List<DocumenTaryStyle> getStyles() {
		return styles;
	}
	public void setStyles(List<DocumenTaryStyle> styles) {
		this.styles = styles;
	}
	public DocumenTaryStyleService getDocumenTaryStyleService() {
		return documenTaryStyleService;
	}
	public void setDocumenTaryStyleService(
			DocumenTaryStyleService documenTaryStyleService) {
		this.documenTaryStyleService = documenTaryStyleService;
	}
	public Integer[] getProconfigIds() {
		return proconfigIds;
	}
	public void setProconfigIds(Integer[] proconfigIds) {
		this.proconfigIds = proconfigIds;
	}
	public CodeNumber getCodeNumber() {
		return codeNumber;
	}
	public void setCodeNumber(CodeNumber codeNumber) {
		this.codeNumber = codeNumber;
	}
	public CodeNumberService getCodeNumberService() {
		return codeNumberService;
	}
	public void setCodeNumberService(CodeNumberService codeNumberService) {
		this.codeNumberService = codeNumberService;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUplodS() {
		return uplodS;
	}

	public void setUplodS(String uplodS) {
		this.uplodS = uplodS;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
}
